﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Beeldbankmvc.Helpers;
using Beeldbankmvc.Models;
using Beeldbankmvc.ViewModels;

namespace Beeldbankmvc.Controllers
{
    [HandleErrorWithELMAH]
    public class PriceInfoController : Controller
    {
        PriceInfoRepository priceinfoRepository = new PriceInfoRepository();

        //
        // GET: /PriceInfo/
        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            var priceinfos = priceinfoRepository.FindAllSorted().ToList();

            return View("Index", priceinfos);
        }

        //
        // GET: /PriceInfo/Details/5
        [Authorize(Roles = "Administrator")]
        public ActionResult Details(int? id)
        {
            if (id == null) {
                return new FileNotFoundResult { Message = "No Price Info found due to invalid id" };
            }
            BEELDBANK_PRIJSINFO priceinfo = priceinfoRepository.FindOne(id.Value);

            if (priceinfo == null)
                return new FileNotFoundResult { Message = "No Price Info found for id=" + id };

            return View(priceinfo);
        }

        //
        // GET: /PriceInfo/Create
        [Authorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            BEELDBANK_PRIJSINFO priceinfo = new BEELDBANK_PRIJSINFO();

            priceinfo.HoofdNummer = 1;
            priceinfo.SubNummer = 1;
            priceinfo.Tonen = true;

            return View(new PriceinfoViewModel(priceinfo)); 
        } 

        //
        // POST: /PriceInfo/Create
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Create(FormCollection collection, BEELDBANK_PRIJSINFO priceinfo)
        {
            if (ModelState.IsValid)
            {
                // Fetching selected item from dropdownlist
                priceinfo.MediaTypeId = Convert.ToInt32(collection["Types"]);
                
                //Add PRICEINFO
                priceinfoRepository.Add(priceinfo);
                priceinfoRepository.Save();

                return RedirectToAction("Details", new { id = priceinfo.Id });
            }
            else
                return View(new PriceinfoViewModel(priceinfo));
        }
        
        //
        // GET: /PriceInfo/Edit/5
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            BEELDBANK_PRIJSINFO priceinfo = priceinfoRepository.FindOne(id);

            if (priceinfo == null)
                return new FileNotFoundResult { Message = "No Price Info found for id=" + id };

            return View(new PriceinfoViewModel(priceinfo));
        }

        //
        // POST: /PriceInfo/Edit/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            // TODO: find out why BEELDBANK_PRIJSINFO priceinfo in the edit method does not return the current price info
            var priceinfo = priceinfoRepository.FindOne(id);

            if (ModelState.IsValid)
            {
                // Selected item from dropdownlist
                priceinfo.MediaTypeId = Convert.ToInt32(collection["Types"]);

                //Save PRICEINFO
                UpdateModel(priceinfo, "Priceinfo", collection); // ,collection
                priceinfoRepository.GetDataContext().SubmitChanges();
                
                return RedirectToAction("Index");
            }
            else
                return View(new PriceinfoViewModel(priceinfo));
        }

        //
        // GET: /PriceInfo/Delete/5
        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(int id)
        {
            BEELDBANK_PRIJSINFO priceinfo = priceinfoRepository.FindOne(id);

            if (priceinfo == null)
                return new FileNotFoundResult { Message = "No Price Info found for id=" + id };

            return View(priceinfo);
        }

        //
        // POST: /PriceInfo/Delete/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            BEELDBANK_PRIJSINFO priceinfo = priceinfoRepository.FindOne(id);

            try
            {
                //Delete PRICEINFO
                priceinfoRepository.Delete(priceinfo);
                priceinfoRepository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        protected override void HandleUnknownAction(string actionName)
        {
            throw new HttpException(404, "Action " + actionName + " not found");
        }
    }
}
